﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace NWBA_Public
{
    public class Account
    {
        public int AccountNumber { get; set; }
        public char AccountType { get; set; }
        public decimal Balance { get; set; }
        public int CustomerID { get; set; }
        public DateTime MDate{ get; set; }
        public Account(int AccountNumber, char AccountType, decimal balance, int ID, DateTime date)
        {
            this.AccountNumber = AccountNumber;
            this.AccountType = AccountType;
            this.Balance = balance;
            this.CustomerID = ID;
            this.MDate = date;
        }
    }
    public class AccountRepostiory
    {
        //public static SqlConnection con = new SqlConnection(ServerString.getServerString());
        public static SqlConnection con = new SqlConnection(ServerString.getServerString());
        public AccountRepostiory()
        {

        }
        public static List<Account> SelectAll()
        {
            //return all accounts regardless of which customer
            List<Account> _Account = new List<Account>();
            con.Open();
            SqlCommand command = new SqlCommand("SELECT AccountNumber, AccountType, Balance, CustomerID, ModifyDate FROM [Account]", con);
            SqlDataReader sqlr = command.ExecuteReader();
            while (sqlr.Read())
            {
                Account newAcct = new Account(sqlr.GetInt32(0), sqlr.GetString(1).ToCharArray()[0], sqlr.GetDecimal(2), sqlr.GetInt32(3), sqlr.GetDateTime(4));
                _Account.Add(newAcct);
            }
            con.Close();
            return _Account;
        }
        public static List<Account> Select(Customer cust)
        {
            //return all account that associate with particular customer
            List<Account> _Account = new List<Account>();
            con.Open();
            SqlCommand command = new SqlCommand("SELECT AccountNumber, AccountType, Balance, CustomerID, ModifyDate FROM [Account] WHERE CustomerID=" + cust.CustomerID, con);
            SqlDataReader sqlr = command.ExecuteReader();
            while (sqlr.Read())
            {
                Account newAcct = new Account(sqlr.GetInt32(0), sqlr.GetString(1).ToCharArray()[0], sqlr.GetDecimal(2), sqlr.GetInt32(3), sqlr.GetDateTime(4));
                _Account.Add(newAcct);
            }
            con.Close();
            return _Account;
        }
        public static List<Account> Select(int id)
        {
            //return all account that associate with particular customer
            List<Account> _Account = new List<Account>();
            con.Open();
            SqlCommand command = new SqlCommand("SELECT AccountNumber, AccountType, Balance, CustomerID, ModifyDate FROM [Account] WHERE CustomerID=" + id, con);
            SqlDataReader sqlr = command.ExecuteReader();
            while (sqlr.Read())
            {
                Account newAcct = new Account(sqlr.GetInt32(0), sqlr.GetString(1).ToCharArray()[0], sqlr.GetDecimal(2), sqlr.GetInt32(3), sqlr.GetDateTime(4));
                _Account.Add(newAcct);
            }
            con.Close();
            return _Account;
        }
        public static Account SelectSingleAccount(int AcctNum)
        {
            try
            {
                con.Open();
                SqlCommand command = new SqlCommand("SELECT AccountNumber, AccountType, Balance, CustomerID, ModifyDate FROM [Account] WHERE AccountNumber=" + AcctNum, con);
                SqlDataReader reader = command.ExecuteReader();
                Account newAcct;
                while (reader.Read())
                {
                    newAcct = new Account(reader.GetInt32(0), reader.GetString(1).ToCharArray()[0], reader.GetDecimal(2), reader.GetInt32(3), reader.GetDateTime(4));
                    return newAcct;
                }
                return null;
            }
            catch (Exception re)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }
        public static Boolean UpdateDeposite(Account acct, decimal amt)
        {
            try
            {
                
                con.Open();
                SqlCommand command = new SqlCommand("UPDATE [Account] set Balance +=@Balance, ModifyDate=@date WHERE AccountNumber=" + acct.AccountNumber, con);
                command.Parameters.AddWithValue("@Balance", amt);
                command.Parameters.AddWithValue("@date", DateTime.Now);
                command.ExecuteNonQuery();                
                return true;
            }
            catch (Exception o)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }
        public static Boolean UpdateWithdraw(Account acct, decimal amt)
        {
            if (acct.AccountType.ToString().ToLower().Equals("s"))
            {
                if (acct.Balance < amt)
                {
                    return false;
                }
            }
            else
            {
                if (acct.Balance < amt + 200)
                {
                    return false;
                }
            }
            try
            {
                con.Open();
                SqlCommand command = new SqlCommand("UPDATE [Account] set Balance -=@Balance, ModifyDate=@date WHERE AccountNumber=" + acct.AccountNumber, con);
                command.Parameters.AddWithValue("@Balance", amt);
                command.Parameters.AddWithValue("@date", DateTime.Now);
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception o)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }
        public static Boolean UpdateTransfer(Account acctFrom, Account acctTo, decimal amt)
        {
            if (!UpdateDeposite(acctTo, amt) || !UpdateWithdraw(acctFrom, amt))
            {
                return false;
            }
            return true;
        }
    }
}